////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) GHI Electronics, LLC.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/* Copyright 2014 Pasi J. Elo, blackdogspark@hotmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 
   This class is heavily modifyed version of GHI's TextBlock class.
 */

using GHI.Glide;
using GHI.Glide.Display;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation.Media;

namespace PJE.GlideEx.UI
{
	public sealed class PagingTexBlock : DisplayObject
	{
		private int _textHeight;
		private string _text;
		private string[] _lines;
		private int _totalLines;
		private int _linesPerPage;
		private int _index;
		private int _start;
		private int _end;

		/// <summary>
		/// Text horizonal alignement within the textfield.
		/// </summary>
		public HorizontalAlignment TextAlign = HorizontalAlignment.Left;

		/// <summary>
		/// Textfield's vertical alignement within the TextBlock.
		/// </summary>
		public VerticalAlignment TextVerticalAlign = VerticalAlignment.Top;

		/// <summary>
		/// Font used by the text.
		/// </summary>
		public Font Font;

		/// <summary>
		/// Indicates the font color.
		/// </summary>
		public Color FontColor = Color.Black;

		/// <summary>
		/// Background color.
		/// </summary>
		public Color BackColor = Color.White;

		/// <summary>
		/// Int containing the total pages
		/// </summary>
		public int TotalPages { get; private set; }

		/// <summary>
		/// String containing the text.
		/// </summary>
		public string Text
		{
			get
			{
				return _text;
			}
			set
			{
				_text = value;

				_textHeight = Font.Height;

				var lines = _text.Split('\n');
				for (var i = 0; i < lines.Length; i += 1)
					lines[i] = lines[i].TrimEnd('\r') + "\n";
				_lines = lines;
				_totalLines = _lines.Length;
				double height = Height;
				double th = _textHeight;
				double d = height / th;
				_linesPerPage = (int)System.Math.Floor(d);
				double tl = _totalLines;
				double lpp = _linesPerPage;
				d = tl / lpp;
				TotalPages = (int)System.Math.Ceiling(d);
				Debug.Print(TotalPages.ToString());
			}
		}

		/// <summary>
		/// Creates a new PagingTextBlock
		/// </summary>
		/// <param name="name"></param>
		/// <param name="alpha"></param>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
		public PagingTexBlock(string name, ushort alpha, int x, int y, int width, int height)
		{
			// Default
			X = x;
			Y = y;
			Width = width;
			Height = height;
			Name = name;
			Alpha = alpha;
			_index = 1;
			_text = string.Empty;
			Font = FontManager.GetFont(FontManager.FontType.droid_reg12);
			_textHeight = Font.Height;
		}

		/// <summary>
		/// Specifyes what page will be shown.
		/// </summary>
		/// <param name="index"></param>
		public void SetPage(int index)
		{
			if (index < 1)
				index = 1;
			else if (index > TotalPages)
				index = TotalPages;

			_index = index;

			_start = (_index - 1) * _linesPerPage;
			_end = _index * _linesPerPage;

			if (_end > _totalLines)
				_end = _totalLines;
		}

		/// <summary>
		/// Renders the PagingTextBlock onto it's parent container's graphics.
		/// </summary>
		public override void Render()
		{
			var x = Parent.X + X;
			var y = Parent.Y + Y;
			var flags = Bitmap.DT_None;
			var textToRender = "";

			for (var i = _start; i < _end; i++)
			{
				textToRender += _lines[i];
			}

			var size = FontManager.GetSize(Font, textToRender);

			switch (TextAlign)
			{
				case HorizontalAlignment.Left:
					flags += Bitmap.DT_AlignmentLeft;
					break;
				case HorizontalAlignment.Center:
					flags += Bitmap.DT_AlignmentCenter;
					break;
				case HorizontalAlignment.Right:
					flags += Bitmap.DT_AlignmentRight;
					break;
			}

			switch (TextVerticalAlign)
			{
				case VerticalAlignment.Middle:
					y += (Height - size.Height) / 2;
					break;
				case VerticalAlignment.Bottom:
					y += Height - size.Height;
					break;
			}

			Parent.Graphics.DrawRectangle(Rect, BackColor, Alpha); // To clear the old text
			Parent.Graphics.DrawTextInRect(textToRender, x, y, Width, Height, flags, FontColor, Font);
		}

		public override TouchEventArgs OnTouchDown(TouchEventArgs e)
		{
			Debug.Print("down y: " + e.Point.Y);
			return base.OnTouchDown(e);
		}
		/*
		public override TouchEventArgs OnTouchUp(TouchEventArgs e)
		{
			Debug.Print("up y: " + e.Point.Y);
			return base.OnTouchUp(e);
		}
		*/
		public override TouchEventArgs OnTouchMove(TouchEventArgs e)
		{
			Debug.Print("move y: " + e.Point.Y);
			return base.OnTouchMove(e);
		}
	}
}
